package dataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JOptionPane;
import javax.swing.JSpinner;

public class Conexao { 
    
    public Connection conn; 

    public Conexao() { 
    	
        try { 
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        } 
        catch (Exception e) { 
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Connection failed.\n", "Error", 0 );            
        } 
        setConn("gauss.grima.ufsc.br",
				"CADSTEPNC" /*bd*/,
				"webcad",
				"julio123");
    } 
  

    public void setConn(String host, String usuario, String senha) { 
        try { 
            conn = DriverManager.getConnection("jdbc:mysql://"+ host +"/grima?user="+ usuario +"&password="+ senha); 
        }        
        catch (SQLException e) { 
            JOptionPane.showMessageDialog(null, "Connection failed.\n", "Error", 0); 
            conn = null; 
        } 
    }

    public void setConn(String host, String db, String usuario, String senha) { 
        try { 
            conn = DriverManager.getConnection("jdbc:mysql://"+ host +"/"+ db +"?user="+ usuario +"&password="+ senha); 
        }        
        catch (SQLException e) { 
            JOptionPane.showMessageDialog(null, "Connection failed.\n", "Error", 0); 
            conn = null; 
        } 
    }
    public String getData(JSpinner p1){
    	Date data = (Date) p1.getValue();
    	String retorno =(1900+data.getYear())+"-"+(1+data.getMonth())+"-"+data.getDate()+"";
		return retorno;
    	
    	
    }

    public void Desconectar() { 
        try { 
            conn.close(); 
			JOptionPane.showMessageDialog(null, "Desconnected!", "Connection", 0);
        } 
        catch (Exception e) { 
            JOptionPane.showMessageDialog(null, "Ocorreram erro ao fechar conexao.\n", "Erro", 0); 
            conn = null; 
        } 
    } 
    
    public Connection getConn() { 
        return conn; 
    } 
} 
